package com.kantboot.business.water.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.kantboot.business.water.domain.dto.BusWaterLiftingPointSearchDTO;
import com.kantboot.business.water.domain.dto.BusWaterLiftingPointSearchFrontDTO;
import com.kantboot.business.water.domain.entity.BusWaterLiftingPoint;
import com.kantboot.business.water.domain.entity.BusWaterUserLiftingPoint;
import com.kantboot.business.water.repository.BusWaterLiftingPointRepository;
import com.kantboot.business.water.repository.BusWaterUserLiftingPointRepository;
import com.kantboot.business.water.service.IBusWaterLiftingPointService;
import com.kantboot.system.user.service.IUserSelfService;
import com.kantboot.util.core.param.PageParam;
import com.kantboot.util.core.result.PageResult;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 提水点服务实现
 */
@Service
public class BusWaterLiftingPointServiceImpl
        implements IBusWaterLiftingPointService {

    @Resource
    private IUserSelfService userSelfService;

    @Resource
    private BusWaterLiftingPointRepository repository;

    @Resource
    private BusWaterUserLiftingPointRepository userLiftingPointRepository;

    /**
     * 现在content是二维数组，所以需要修改，变成一维数组，只取下标为0的元素
     * @param pageResult
     * @return PageResult
     */
    private PageResult changeContent(PageResult pageResult) {
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(pageResult));
        // 获取jsonObject的content
        JSONArray content = jsonObject.getJSONArray("content");

        // 创建一个新的JSONArray
        JSONArray newContent = new JSONArray();

        // 遍历content
        for (int i = 0; i < content.size(); i++) {
            // 获取content的第i个元素
            JSONArray item = content.getJSONArray(i);
            // 获取item的第0个元素
            newContent.add(item.get(0));
        }
        // 设置jsonObject的content为newContent
        jsonObject.put("content", newContent);

        return JSON.parseObject(jsonObject.toJSONString(), PageResult.class);
    }


    @Override
    public PageResult getBodyData(PageParam<BusWaterLiftingPointSearchDTO> pageParam) {
        PageResult pageResult = PageResult.of(repository.getBodyData(pageParam.getData(), pageParam.getPageable()));
        return changeContent(pageResult);
    }

    @Override
    public PageResult getBodyDataFront(PageParam<BusWaterLiftingPointSearchFrontDTO> pageParam) {
        PageResult pageResult = PageResult.of(repository.getBodyDataFront(pageParam.getData(), pageParam.getPageable()));
        return changeContent(pageResult);
    }

    @Override
    public List<BusWaterLiftingPoint> getListByUserId(Long userId) {
        return repository.getByUserId(userId);
    }

    @Override
    public List<BusWaterLiftingPoint> getSelfList() {
        Long selfId = userSelfService.getSelfId();
        return getListByUserId(selfId);
    }

    @Override
    public BusWaterLiftingPoint getById(Long id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public void bindLiftingPoint(Long userId, List<Long> liftingPointIds) {
        // 删除用户的所有提水点
        userLiftingPointRepository.deleteByUserId(userId);
        // 给用户绑定提水点
        List<BusWaterUserLiftingPoint> liftingPoints = new ArrayList<>();
        for (Long pointId : liftingPointIds) {
            liftingPoints.add(new BusWaterUserLiftingPoint().setUserId(userId).setLiftingPointId(pointId));
        }
        userLiftingPointRepository.saveAll(liftingPoints);
    }
}
